﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
using System.Data;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

public partial class wenjuan : WeiPage
{
    
    PersonInfoBO personbo = new PersonInfoBO();
    wenjuanClass wjclass = new wenjuanClass();
    wenjuanBO wjbo = new wenjuanBO();
    protected void Page_Load(object sender, EventArgs e)
    {

       if (!IsPostBack)
        {
            if (Request.QueryString["code"] != null)
            {
                try
                {
                    string url = string.Format(@"https://api.weixin.qq.com/sns/oauth2/access_token?appid="+GetAppid()+"&secret="+GetSecret()+"&code={0}&grant_type=authorization_code", Request.QueryString["code"].ToString());
                    string token = PostRequest(url);
                    if (token.Contains("7200"))
                    {
                        string[] b = token.Split('\"');
                        Session["FromUserName"] = b[13];
                        Session["ToUserName"] = GetAppid();
                      
                    }
                }
                catch (Exception)
                {

                }
            }

            if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
            {
                string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
                string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
                Session["FromUserName"] = user;
                Session["ToUserName"] = user2;
            }

            string userid = (string)Session["FromUserName"];
            if (userid != null)
            {

                int count = personbo.GetByFromUserName(userid);
                
                if (count > 1)
                {
                    throw new Exception("存在重复的微信号");

                }
                else if (count <= 0)
                {
                    Response.Redirect("~/MembershipCard/wechat_web/zc.aspx");
                }

                wjclass.UserId = userid;
                if (wjbo.Exists(wjclass.UserId))
                {
                    Response.Redirect("~/MembershipCard/wechat_web/wd.aspx");
                    AlertMessage("您已提交过问卷，谢谢");
                }

            }


        }
    }


    protected void wenjuan_sub_Click(object sender, EventArgs e)
    {
          wjclass.UserId = (string)Session["FromUserName"];

          if (wjclass.UserId != null)
          {
              DataSet ds = personbo.GetNameByFromUserName(wjclass.UserId);
              if (ds.Tables[0] != null)
              {
                  if (ds.Tables[0].Rows.Count > 0)
                  {
                      DataRow dr = ds.Tables[0].Rows[0];
                      if (dr != null)
                      {
                          wjclass.Name = dr["Name"].ToString();
                          wjclass.Addr = dr["addr"].ToString();
                          wjclass.Phone = dr["Phone"].ToString();
                          
                      }
                  }
              }
              ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;  
              string url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + Token() + "&openid=" + wjclass.UserId;
              string b = PostRequest(url);
              string[] a = b.Split('\"');
              if (a.Length == 35)
              wjclass.WeiXinName= a[9].ToString();
          }

        //wjclass.UserId = userid;
        if (wjbo.Exists(wjclass.UserId))
        {
            Response.Redirect("~/MembershipCard/wechat_web/wd.aspx");
            //AlertRedirect("问卷回答完毕，谢谢合作！", "MembershipCard/wechat_web/wd.aspx");
        }
      
            wjclass.AnswerTime = DateTime.Now;
            //第一题
            if (Q1O1.Checked)
            {
                wjclass.Ans1 = Q1O1.Value;
            }
            else if (Q1O2.Checked)
            {
                wjclass.Ans1 = Q1O2.Value;
            }
            else if (Q1O3.Checked)
            {
                wjclass.Ans1 = Q1O3.Value;
            }
            else if (Q1O4.Checked)
            {
                wjclass.Ans1 = Q1O4.Value;
            }

            //第二题
            if (Q2O1.Checked)
            {
                wjclass.Ans2 = Q2O1.Value;
            }
            else if (Q2O2.Checked)
            {
                wjclass.Ans2 = Q2O2.Value;
            }
            else if (Q2O3.Checked)
            {
                wjclass.Ans2 = Q2O3.Value;
            }
            else if (Q2O4.Checked)
            {
                wjclass.Ans2 = Q2O4.Value;
            }

            //第三题
            if (Q3O1.Checked)
            {
                wjclass.Ans3 = Q3O1.Value;
            }
            else if (Q3O2.Checked)
            {
                wjclass.Ans3 = Q3O2.Value;
            }
            else if (Q3O3.Checked)
            {
                wjclass.Ans3 = Q3O3.Value;
            }
            else if (Q3O4.Checked)
            {
                wjclass.Ans3 = Q3O4.Value;
            }

            //第四题
            if (Q4O1.Checked)
            {
                wjclass.Ans4 = Q4O1.Value;
            }
            else if (Q4O2.Checked)
            {
                wjclass.Ans4 = Q4O2.Value;
            }
            else if (Q4O3.Checked)
            {
                wjclass.Ans4 = Q4O3.Value;
            }
            else if (Q4O4.Checked)
            {
                wjclass.Ans4 = Q4O4.Value;
            }

            //第五题
            if (Q5O1.Checked)
            {
                wjclass.Ans5 = Q5O1.Value;
            }
            else if (Q5O2.Checked)
            {
                wjclass.Ans5 = Q5O2.Value;
            }
            else if (Q5O3.Checked)
            {
                wjclass.Ans5 = Q5O3.Value;
            }


            //第六题
            if (Q6O1.Checked)
            {
                wjclass.Ans6 = Q6O1.Value;
            }
            else if (Q6O2.Checked)
            {
                wjclass.Ans6 = Q6O2.Value;
            }
            else if (Q6O3.Checked)
            {
                wjclass.Ans6 = Q6O3.Value;
            }

            wjclass.Ans7 = advice7.Value;
            wjclass.Ans8 = advice8.Value;
            wjclass.Anscount = 1;

            wjbo.Add(wjclass);
           // Response.Write("<script language='javascript'>window.alert('问卷回答完毕，谢谢合作！')</script>");
            //AlertRedirect("问卷回答完毕，谢谢合作！", "MembershipCard/wechat_web/wd.aspx");
            Response.Redirect("~/MembershipCard/wechat_web/wd.aspx");
        }


    private bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        return true;
    }  
    }
       
                               
    

